package com.ssy.lingxi.marketing.serviceimpl.feign;

import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.model.dto.MemberAndRoleIdDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.service.feign.IMemberFeignService;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberInnerControllerFeign;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberLevelRightCreditControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.*;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 会员内部接口
 * @author yzc
 * @version 2.0.0
 * @date 2021/7/19
 */
@Service
public class MemberFeignServiceImpl implements IMemberFeignService {

    @Resource
    private MemberInnerControllerFeign memberInnerControllerFeign;
    @Resource
    private MemberLevelRightCreditControllerFeign memberLevelRightCreditControllerFeign;

    @Override
    public Wrapper<List<MemberFeignQueryVO>> listPlatformMembers(List<MemberFeignVO> memberFeignVOList) {
        return memberInnerControllerFeign.listPlatformMembers(memberFeignVOList);
    }

    @Override
    public Wrapper<List<MemberFeignQueryVO>> listLowerMembers(MemberFeignSubVO feignVO) {
        return memberInnerControllerFeign.listLowerMembers(feignVO);
    }

    @Override
    public Wrapper<List<MemberFeignLevelConfigVO>> getMemberLevelConfigBatch(List<Long> memberLevelConfigId) {
        return memberLevelRightCreditControllerFeign.getMemberLevelConfigBatch(memberLevelConfigId);
    }

    @Override
    public Wrapper<List<AtSubMemberSuitableMemberVO>> listAbilitySubMemberSuitableMember(MemberAndUpperMembersVO memberAndUpperMembersVO) {
        return memberInnerControllerFeign.listAbilitySubMemberSuitableMember(memberAndUpperMembersVO);
    }

    @Override
    public Wrapper<PfSubMemberSuitableMemberVO> getPlatformSubMemberSuitableMember(MemberFeignVO memberFeignVO) {
        return memberInnerControllerFeign.getPlatformSubMemberSuitableMember(memberFeignVO);
    }

    @Override
    public Wrapper<MemberFeignRightVO> getMemberPriceRight(MemberFeignRelationVO relationVO) {
        return memberLevelRightCreditControllerFeign.getMemberPriceRight(relationVO);
    }

    @Override
    public Wrapper<List<MemberFeignRightDetailVO>> batchMemberPriceRight(MemberFeignBatchVO batchVO){
        return memberLevelRightCreditControllerFeign.batchMemberPriceRight(batchVO);
    }

    @Override
    public Wrapper<List<MemberFeignLogoVO>> getMemberLogos(List<Long> memberIds){
        return memberInnerControllerFeign.getMemberLogos(memberIds);
    }

    /**
     * 调用会员服务-查询权益参数值
     * @param memberId
     * @param roleId
     * @param upMemberLis
     * @return key=会员id_角色id
     */
    @Override
    public Map<String,Double> getMemberPriceRight(Long memberId, Long roleId, List<MemberAndRoleIdDTO> upMemberLis){
        Map<String,Double> map=new HashMap<>();
        for(MemberAndRoleIdDTO upperMember :upMemberLis) {
            MemberFeignRelationVO vo = new MemberFeignRelationVO();
            vo.setSubMemberId(memberId);
            vo.setSubRoleId(roleId);
            vo.setUpperMemberId(upperMember.getMemberId());
            vo.setUpperRoleId(upperMember.getRoleId());
            Wrapper<MemberFeignRightVO> memberPriceRight = getMemberPriceRight(vo);
            if(memberPriceRight!=null&&memberPriceRight.getCode()==Wrapper.success().getCode()){
                MemberFeignRightVO data = memberPriceRight.getData();
                //如果未查询到权益，默认返回1D
                double priceRight=1D;
                if(data!=null) {
                    priceRight = data.getParameter().doubleValue();
                    //如果停用权益返回1
                    if (EnableDisableStatus.DISABLE.getCode().equals(data.getStatus())) {
                        priceRight = 1D;
                    }
                }
                map.put(upperMember.getMemberId()+"_"+upperMember.getRoleId(),priceRight);
            }
        }
        return map;
    }

    public Boolean isSubMember(MemberRelationFeignVO feignVO){
        Wrapper<Boolean> subMember = memberInnerControllerFeign.isSubMember(feignVO);
        return subMember.getData();
    }

    public Wrapper<List<MemberFeignQueryVO>> listAllLowerMembers(MemberFeignVO feignVO){
        return memberInnerControllerFeign.listAllLowerMembers(feignVO);
    }

}